package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.MenuDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService iSysMenuService;

    /**
     * 所有菜单
     * @return
     */
    @GetMapping("/allData")
    public ResultData allData(){
        MenuDto menuDto = new MenuDto();
        menuDto.setId(0);
        List<MenuDto> menuList = iSysMenuService.selectAll(menuDto,null);
        return ResultData.ok("",menuList);
    }

    /**
     * 修改时数据回显
     *
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getRowInfo(SysMenu sysMenu) {

        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(sysMenu.getId() != null, SysMenu::getId, sysMenu.getId());
        SysMenu sysMenuInfo = iSysMenuService.getOne(wrapper);
        return ResultData.ok("",sysMenuInfo);

    }

    /**
     * 新增
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody SysMenu sysMenu){
        log.info("新增操作");
        iSysMenuService.save(sysMenu);
        return ResultData.ok("添加成功",sysMenu);
    }

    /**
     * 修改
     * @param sysMenu
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody SysMenu sysMenu){
        log.info("修改信息:{}",sysMenu);
        iSysMenuService.updateById(sysMenu);
        return ResultData.ok("修改成功",sysMenu);
    }

    /**
     * 删除
     * @param sysMenu
     * @return
     */
    @RequestMapping("/del")
    public ResultData deleteById(SysMenu sysMenu){
        iSysMenuService.removeById(sysMenu.getId());
        return ResultData.ok("删除成功",sysMenu);
    }

}
